package org.assistant.csp.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.assistant.AssistantException;
import org.assistant.common.exception.GetFromDBException;
import org.assistant.common.exception.SaveIntoDBException;
import org.assistant.common.jdbc.core.RowMapper;
import org.assistant.common.jdbc.dao.BasicDao;
import org.assistant.csp.data.Home;

public class HomeDaoImpl extends BasicDao implements HomeDao {
	private class HomeRowMapper implements RowMapper<Home> {
		public Home mapRow(ResultSet rs, int index) throws SQLException {
			Home home = new Home();
			home.setId(rs.getString("home_id_"));
			home.setName(rs.getString("home_name_"));
			home.setCreater(rs.getString("creater_"));
			home.setCreateDate(rs.getDate("create_date_"));

			return home;
		}
	}

	/**
	 * 插入一条家庭信息
	 * 
	 * @param createrId
	 *            -创建者ID
	 * @param homeId
	 *            -家庭ID
	 * @param homeName
	 *            -家庭名称
	 * @return Home
	 * @throws SaveIntoDBException
	 * @throws GetFromDBException
	 */
	public Home insertHome(String createrId, String homeId, String homeName)
			throws SaveIntoDBException, GetFromDBException {
		StringBuffer insertSqlBuff = new StringBuffer(
			"insert into common_home(");
		insertSqlBuff.append("home_id_,home_name_,creater_,create_date_) ");
		insertSqlBuff.append("values(?,?,?,now())");

		int rows;
		try {
			rows = getJdbcTemplate().update(
				insertSqlBuff.toString(), homeId, homeName, createrId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SaveIntoDBException("保存家庭信息时出错啦：" + e.getMessage());
		}

		if (rows <= 0) {
			throw new SaveIntoDBException("保存家庭信息时出错啦！");
		}

		return getHome(homeId);
	}

	/**
	 * 修改一条家庭信息
	 * 
	 * @param home
	 * @throws SaveIntoDBException
	 */
	public void updateHome(Home home) throws SaveIntoDBException {
		StringBuffer sqlBuff = new StringBuffer("update common_home set ");
		sqlBuff.append("home_name_=? ");
		sqlBuff.append("where home_id_=?");

		int rows;
		try {
			rows = getJdbcTemplate().update(
				sqlBuff.toString(), home.getName(), home.getId());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SaveIntoDBException("修改家庭信息时出错啦：" + e.getMessage());
		}

		if (rows <= 0) {
			throw new AssistantException("修改家庭信息时出错啦！");
		}
	}

	/**
	 * 根据homeId获取家庭信息
	 * 
	 * @param homeId
	 * @return
	 * @throws GetFromDBException
	 */
	public Home getHome(String homeId) throws GetFromDBException {
		StringBuffer getHomeSqlBuff = new StringBuffer("select home_id_,");
		getHomeSqlBuff
			.append("home_name_,creater_,create_date_ from common_home ");
		getHomeSqlBuff.append("where home_id_=?");

		Home home;
		try {
			home = getJdbcTemplate().queryForObject(
				getHomeSqlBuff.toString(), new HomeRowMapper(), homeId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new GetFromDBException("查询家庭信息时出错啦：" + e.getMessage());
		}
		return home;
	}
}
